home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / dejagnu / example / calc / calc.c < prev    next >
C/C++ Source or Header  |  1993-05-05  |  1KB  |  65 lines

  1. #ifdef HAVE_STDLIB_H
  2. #include <stdlib.h>
  3. #endif
  4. #include <stdio.h>
  5. #include "calc.h"
  6.  
  7. static int words();
  8.  
  9. int main()
  10. {
  11.    char line[SIZE];
  12.    int nword;
  13.    char *words[NWORD];
  14.  
  15.    while(printf("calc: "), fflush(stdout), fgets(line,SIZE,stdin) != NULL) {
  16.       if((nword = split(line,words,NWORD)) == 0) continue;
  17.       if(strcmp(words[0],"add") == 0) {
  18.      if(nword != 3) {
  19.         printf("Usage: add #1 #2\n");
  20.      } else {
  21.         printf("%d",atoi(words[1]) + atoi(words[2]));
  22.      }
  23.       } else if(strcmp(words[0],"multiply") == 0) {
  24.      if(nword != 3) {
  25.         printf("Usage: multiply #1 #2\n");
  26.      } else {
  27.         int i1 = atoi(words[1]);
  28.         if(i1 == 2) i1 = 3;        /* this is a bug */
  29.         printf("%d",i1*atoi(words[2]));
  30.      }
  31.       } else if(strcmp(words[0],"quit") == 0) {
  32.      break;
  33.       } else if(strcmp(words[0],"version") == 0) {
  34.      printf("Version: %s",VERSION);
  35.       } else {
  36.      printf("Unknown command: %s",words[0]);
  37.       }
  38.       printf("\n");
  39.    }
  40.  
  41.    return(0);
  42. }
  43.  
  44. int
  45. split(line,words,nword)
  46. char *line;
  47. char **words;
  48. int nword;                /* number of elements in words */
  49. {
  50.    int i;
  51.  
  52.    while(isspace(*line)) line++;
  53.    if(*line == '\0') return(0);
  54.  
  55.    for(i = 0;i < nword;i++) {
  56.       words[i] = line;
  57.       while(*line != '\0' && !isspace(*line)) line++;
  58.       if(*line == '\0') break;
  59.       *line++ = '\0';
  60.       while(isspace(*line)) line++;
  61.    }
  62.  
  63.    return(i);
  64. }
  65.